<template>
  <div>
    <div>
      <ele-page
        v-loading="loading"
        :formConfig="searchDialog"
        ref="eleTable"
        :operateList="operateList"
        @handleOperateEvent="handleOperateEvent"
        :column="column"
        :getList="getList"
        uniqueId="mesScanKeypartsTable"
      >
      </ele-page>
    </div>
    <!-- <ele-form-drawer
      class="dialog-pack"
      :isLoading="isLoading"
      v-model="formDataDialog"
      v-bind="formDescDialog"
      :request-fn="handleSubmit"
      :visible.sync="visible"
      :title="title"
    >
    </ele-form-drawer> -->
  </div>
</template>

<script>
import { operateList, searchConfig, formDescDialog, rowConfig } from './config'
import {
  getList,
  rowCreate,
  rowUpdate,
  rowDel,
  rowDetail,
  getLineCodeList,
  getProcessCodeList,
  getWorkCellCodeList,
  getWorkTeamCodeList,
  getWorkShiftCodeList,
  getWorkOrderCodeList,
  getWorkOrderNodeList
} from '@/api/report/mesScanKeyparts'

export default {
  name: 'mesScanKeyparts',
  mixins: [searchConfig],
  data() {
    return {
      title: null,
      visible: false,
      isLoading: false,
      loading: false,
      formDataDialog: {},
      formDescDialog, //新增修改框
      // searchConfig,
      operateList,
      getList,
      // 列表页字段的绑定配置
      column: [
        {
          type: 'selection',
          fixed: 'left'
        },
        {
          type: 'index',
          label: $i18n.t('common.order'),
          width: 60,
          fixed: 'left'
        },
        {
          prop: 'barcode',
          label: $i18n.t('mesScanKeyParts.barCode'),
          minWidth: 220,
          showOverflowTooltip: true
        },
        {
          prop: 'orderCode',
          label: $i18n.t('mesScanKeyParts.orderCode'),
          minWidth: 220,
          showOverflowTooltip: true
        },
        {
          prop: 'seriesCode',
          label: $i18n.t('mesScanKeyParts.seriesCode'),
          minWidth: 120,
          showOverflowTooltip: true
        },
        {
          prop: 'seriesName',
          label: $i18n.t('mesScanKeyParts.seriesName'),
          minWidth: 120,
          showOverflowTooltip: true
        },
        {
          prop: 'prodCode',
          label: $i18n.t('mesScanKeyParts.prodCode'),
          minWidth: 140,
          showOverflowTooltip: true
        },
        {
          prop: 'prodName',
          label: $i18n.t('mesScanKeyParts.prodName'),
          minWidth: 160,
          showOverflowTooltip: true
        },
        {
          prop: 'materialCode',
          label: $i18n.t('mesScanKeyParts.materialCode'),
          minWidth: 160,
          showOverflowTooltip: true
        },
        {
          prop: 'materialName',
          label: $i18n.t('mesScanKeyParts.materialName'),
          minWidth: 160,
          showOverflowTooltip: true
        },
        {
          prop: 'keypartCode',
          label: $i18n.t('mesScanKeyParts.keyPartCode'),
          minWidth: 200,
          showOverflowTooltip: true
        },
        {
          prop: 'status',
          label: $i18n.t('mesScanKeyParts.status'),
          render: (h, scope) => {
            const status = scope.row.status == '1' ? $i18n.t('mesScanKeyParts.bind') : $i18n.t('mesScanKeyParts.unbind')
            return <el-tag type={scope.row.status == '1' ?'success':'info'}>{status}</el-tag>
          },
          minWidth: 120,
          showOverflowTooltip: true
        },
        {
          prop: 'lineCode',
          label: $i18n.t('common.lineCode'),
          minWidth: 120,
          showOverflowTooltip: true
          // optionProp: {
          //   value: "value",
          //   label: "description",
          // },
          // option: async (data) => {
          //   const list = await getLineCodeList()
          //   return list.data.result
          // },
        },
        {
          prop: 'lineName',
          label: $i18n.t('common.lineName'),
          minWidth: 160,
          showOverflowTooltip: true
        },
        {
          prop: 'processCode',
          label: $i18n.t('common.processCode'),
          minWidth: 120,
          showOverflowTooltip: true
          // optionProp: {
          //   value: "value",
          //   label: "description",
          // },
          // option: async (data) => {
          //   const list = await getProcessCodeList()
          //   return list.data.result
          // },
        },
        {
          prop: 'processName',
          label: $i18n.t('common.processName'),
          minWidth: 120,
          showOverflowTooltip: true
        },
        {
          prop: 'workCellCode',
          label: $i18n.t('mesScanKeyParts.workCellCode'),
          minWidth: 180,
          showOverflowTooltip: true
          // optionProp: {
          //   value: "value",
          //   label: "description",
          // },
          // option: async (data) => {
          //   const list = await getWorkCellCodeList()
          //   return list.data.result
          // },
        },
        {
          prop: 'workCellName',
          label: $i18n.t('mesScanKeyParts.workCellName'),
          minWidth: 180,
          showOverflowTooltip: true
        },
        {
          prop: 'workTeamCode',
          label: $i18n.t('mesScanKeyParts.workTeamCode'),
          minWidth: 180,
          showOverflowTooltip: true
          // optionProp: {
          //   value: "value",
          //   label: "description",
          // },
          // option: async (data) => {
          //   const list = await getWorkTeamCodeList()
          //   return list.data.result
          // },
        },
        {
          prop: 'workTeamName',
          label: $i18n.t('mesScanKeyParts.workTeamName'),
          minWidth: 180,
          showOverflowTooltip: true
        },
        {
          prop: 'workShiftCode',
          label: $i18n.t('mesScanKeyParts.workShiftCode'),
          minWidth: 180,
          showOverflowTooltip: true
          // optionProp: {
          //   value: "value",
          //   label: "description",
          // },
          // option: async (data) => {
          //   const list = await getWorkShiftCodeList()
          //   return list.data.result
          // },
        },
        {
          prop: 'workShiftName',
          label: $i18n.t('mesScanKeyParts.workShiftName'),
          minWidth: 180,
          showOverflowTooltip: true
        },
        // {
        //   prop: 'userCode',
        //   label: $i18n.t('mesScanKeyParts.userCode'),
        //   minWidth: 220,
        //   showOverflowTooltip: true
        // },
        {
          prop: 'userName',
          label: $i18n.t('mesScanKeyParts.userName'),
          minWidth: 140,
          showOverflowTooltip: true
        },
        {
          prop: 'scanTime',
          label: $i18n.t('mesScanKeyParts.scanTime'),
          minWidth: 180,
          showOverflowTooltip: true
        },
        // {
        //   prop: 'workOrderCode',
        //   label: $i18n.t('mesScanKeyParts.workOrderCode'),
        //   minWidth: 200,
        //   showOverflowTooltip: true
        //   // optionProp: {
        //   //   value: "value",
        //   //   label: "description",
        //   // },
        //   // option: async (data) => {
        //   //   const list = await getWorkOrderCodeList()
        //   //   return list.data.result
        //   // },
        // },
        // {
        //   prop: 'workOrderNode',
        //   label: $i18n.t('mesScanKeyParts.workOrderName'),
        //   minWidth: 200,
        //   showOverflowTooltip: true
        //   // optionProp: {
        //   //   value: "value",
        //   //   label: "description",
        //   // },
        //   // option: async (data) => {
        //   //   const list = await getWorkOrderNodeList()
        //   //   return list.data.result
        //   // },
        // },
        // {
        //   prop: 'factoryCode',
        //   label: $i18n.t('common.factoryCode'),
        //   minWidth: 120,
        //   showOverflowTooltip: true
        // },
        {
          prop: 'Remark',
          label: $i18n.t('common.remark'),
          showOverflowTooltip: true
        },
        {
          prop: 'createByName',
          label: $i18n.t('common.createByName'),
          width: 140,
          showOverflowTooltip: true
        },
        {
          prop: 'createTime',
          label: $i18n.t('common.createTime'),
          width: 170
        },
        {
          prop: 'updateByName',
          label: $i18n.t('common.updateByName'),
          width: 140,
          showOverflowTooltip: true
        },
        {
          prop: 'updateTime',
          label: $i18n.t('common.updateTime'),
          width: 170
        }
        // {
        //   prop: "operate",
        //   label: "操作",
        //   width: 160,
        //   render: (h, scope) => {
        //     return (
        //       <ele-row-operation
        //         scope={scope}
        //         rowConfig={rowConfig}
        //         onRowOperate={this.rowOperate}
        //       />
        //     )
        //   },
        // },
      ]
    }
  },
  computed: {},
  methods: {
    //表格顶部按钮操作事件
    handleOperateEvent(data) {
      this.title = data.name
      const fnc = {
        add: () => {
          this.disableFields(false)
          this.formDataDialog = {}
          this.visible = true
        },
        deleteBatch: () => {
          this.$confirm($i18n.t('common.deleteBatch',{name:$i18n.t('mesScanKeyParts.name')}), $i18n.t('common.tip'), {
            confirmButtonText: $i18n.t("common.ok"),
            cancelButtonText: $i18n.t("common.cancel"),
            type: 'warning'
          }).then(() => {
            const ids = data.rows.map((val) => val.id).join(',')
            this.rowDel(ids)
          })
        }
      }
      fnc[data.handleName]()
    },
    //表格行操作事件
    rowOperate({ handleName, name, row }) {
      this.title = name
      const fnc = {
        edit: () => {
          this.disableFields(false)
          this.rowDetail(row.id)
          this.visible = true
        },
        detail: () => {
          this.disableFields(true)
          this.rowDetail(row.id)
          this.visible = true
        },
        delete: () => {
          this.$confirm($i18n.t('common.deleteSingle',{name:$i18n.t('mesScanKeyParts.name')}), $i18n.t('common.tip'), {
            confirmButtonText: $i18n.t('common.ok'),
            cancelButtonText: $i18n.t('common.cancel'),
            type: 'warning'
          }).then(() => {
            this.rowDel(row.id)
          })
        }
      }
      fnc[handleName]()
    },
    doSubmit({ title, api, data }) {
      this.isLoading = true
      api(data)
        .then((res) => {
          const message = res.data.message
          this.$message.success(message || title)
          this.visible = false
          this.refresh()
        })
        .finally(() => {
          this.isLoading = false
        })
    },
    //弹框提交事件
    handleSubmit(data) {
      if (this.isLoading) return // 判断状态
      const params = this.formDataDialog.id
        ? { title: $i18n.t('common.editSuccess'), api: rowUpdate, data }
        : { title: $i18n.t('common.addSuccess'), api: rowCreate, data }
      this.doSubmit(params)
    },
    //删除
    rowDel(data) {
      this.loading = true
      rowDel(data)
        .then((res) => {
          const message = res.data.message
          this.$message.success(message || $i18n.t("common.deleteSuccess"))
          this.refresh()
        })
        .finally(() => {
          this.loading = false
        })
    },
    disableFields(flag) {
      this.formDescDialog.disabled = flag
      this.formDescDialog.isShowSubmitBtn = !flag
    },
    rowDetail(id) {
      rowDetail(id).then((res) => {
        this.formDataDialog = res.data.result
      })
    },
    //刷新表格数据
    refresh(isReset = true) {
      this.$refs.eleTable.refresh(isReset)
    }
  }
}
</script>
